import { defineStore } from 'pinia';

export const useLoadingStore = defineStore('loading', {
	state: () => ({
		show: false,
		info: { title: '', time: 0, color: '#fff', maskColor: false, mask: false }
	}),
	actions: {
		showLoading(info : any) {
			console.log('开启')
			this.show = true;
			this.info = info
			if (this.info.time) {
				this.info.interval = setInterval(() => {
					this.info.time -= 1;
					if (this.info.time < 1) {
						this.info.time = '';
						clearInterval(this.info.interval);
					}
				}, 1000)
			}
		},
		hideLoading() {
			console.log('关闭')
			this.show = false;
			if (this.info.interval) {
				clearInterval(this.info.interval);
			}
		}
	},
	// persist:{
	//     storage:{
	// 		getItem: uni.getStorageSync,
	// 		setItem: uni.setStorageSync
	// 	},
	// 	key:'loadingInfo',//本地存储key值
	// 	paths:['show','info']// 指定持久化的数据，不写默认持久化整个state
	// }
});

//买家已选地址等
interface userState {
	// 具有索引签名的对象，属性不确定
	curSelecAdress: Record<string, any>;
	zitPhone:any;//自取电话
	coups:any;
	vipCoups:any;
	changeAdress:any;
	userInfo:any;
	changeDCarriages:boolean;
}

// 示例
export const useUserStore = defineStore('user', {
	state: (): userState => ({
		curSelecAdress:{},
		changeAdress:[false,false],
		zitPhone:'',
		coups:{
			count:0,
			status:false
		},
		vipCoups:{
			count:0,
			status:false
		},
		userInfo:{},
		changeDCarriages:false
	}),
	getters: {
		getAdresGps: (state) => {
			return {
				hasGps:state.curSelecAdress.latitude&&state.curSelecAdress.longitude?true:false,
				latitude:state.curSelecAdress.latitude||'',
				longitude:state.curSelecAdress.longitude||''
			} 
		} 
	},
	actions: {
		setSelectAdress(info:any={}){
			this.curSelecAdress = info;
		},
		// setMealsAdress(info:any){
		// 	this.curMealsAdress = info;//多套-设置地址
		// },
		setZitPhone(num:any){
		  this.zitPhone = num
		},
		setCoupsCount(count:any){
			this.coups.count=count
		},
		setCoupsStatus(status:any){
			this.coups.status=status
		},
		setVipCoupsCount(count:any){
			this.vipCoups.count=count
		},
		setVipCoupsStatus(status:any){
			this.vipCoups.status=status
		},
		setUserInfo(info:any){
			console.log('setUserInfo',info)
			this.userInfo = info
		},
		setChangeAdress(v:any){
			!Array.isArray(this.changeAdress)?this.changeAdress = [false,false]:'';
			this.changeAdress[0] = v;
			v?this.changeDCarriages = true:''
			
		},
		setDCarriages(v){
			this.changeDCarriages = v
		},
		// 暂无使用
		setNeedCaclCarriage(v){
			this.changeAdress[1] = v
		}
		//异步使用
		// async fetchUsers() {
		// 	this.isLoading = true;
		// 	try {
		// 		// 假设getUsers是一个异步API调用
		// 		const users = await getUsers();
		// 		this.users = users;
		// 	} catch (error) {
		// 		console.error('Fetching users failed:', error);
		// 		// 处理错误，例如设置错误消息
		// 	} finally {
		// 		this.isLoading = false;
		// 	}
		// }
	},
  persist:{
      storage:{
  		getItem: uni.getStorageSync,
  		setItem: uni.setStorageSync
  	},
  	key:'userInfos',//本地存储key值
  	paths:['curSelecAdress','zitPhone','coups','vipCoups','userInfo','changeAdress']// 指定持久化的数据，不写默认持久化整个state
  }
});